/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.ProfessorDao;
import Dao.ProfessorDaoImplJPA;
import Dao.TurmaDao;
import Dao.TurmaDaoImplJPA;
import Pojo.Professor;
import Pojo.Turma;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author thiago
 */
public class FormCadastroProfessores extends javax.swing.JInternalFrame {

    String modificacao;

    /**
     * Setter de modificação
     * @param modificacao 
     */
    public void setModificacao(String modificacao) {
        this.modificacao = modificacao;
    }

    /**
     * Getter de modificação
     * @return 
     */
    public String getModificacao() {
        return modificacao;
    }
    
    /**
     * Creates new form FormCadastroProfessores
     */
    public FormCadastroProfessores() {
        initComponents();
    }
    
    /**
     * Creates new form FormCadastroProfessores
     */
    public FormCadastroProfessores(int id) {
        initComponents();
        txtCodigo.setText(String.valueOf(id));
        this.carregarDadosCampos(id);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtCpf = new javax.swing.JTextField();
        lblCpf = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        lblNome = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        lblCodigo = new javax.swing.JLabel();
        lblDepartamento = new javax.swing.JLabel();
        txtDepartamento = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        btnNovo = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnRemover = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Cadastro de Professores");

        txtCpf.setEnabled(false);

        lblCpf.setText("CPF");

        txtNome.setEnabled(false);

        lblNome.setText("Nome");

        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodigoKeyPressed(evt);
            }
        });

        lblCodigo.setText("Código");

        lblDepartamento.setText("Departamento");

        txtDepartamento.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNome)
                    .addComponent(txtCpf)
                    .addComponent(txtDepartamento)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNome)
                            .addComponent(lblCpf)
                            .addComponent(lblCodigo)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblDepartamento))
                        .addGap(0, 86, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCodigo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblCpf)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblDepartamento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnAlterar.setText("Alterar");
        btnAlterar.setEnabled(false);
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnRemover.setText("Remover");
        btnRemover.setEnabled(false);
        btnRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoverActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.setEnabled(false);
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setEnabled(false);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNovo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAlterar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnRemover, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAlterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnRemover)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void carregarDadosCampos(int codigo){
        ProfessorDao professorDao = new ProfessorDaoImplJPA();
        Professor professor = professorDao.pesquisarPorId(codigo);

        if (professor != null){
            txtNome.setText(professor.getNome());
            txtCpf.setText(professor.getCpf());
            txtDepartamento.setText(professor.getDepartamento());

            btnNovo.setEnabled(true);
            btnAlterar.setEnabled(true);
            btnRemover.setEnabled(true);
            btnSalvar.setEnabled(false);
            btnCancelar.setEnabled(false);
        } else {
            JOptionPane.showMessageDialog(null, "Não existe um professor com esse código.");
            txtNome.setText("");
            txtCpf.setText("");
            txtDepartamento.setText("");

            btnNovo.setEnabled(true);
            btnAlterar.setEnabled(false);
            btnRemover.setEnabled(false);
            btnSalvar.setEnabled(false);
            btnCancelar.setEnabled(false);
        }
    }
    
    private void txtCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyPressed
        if (evt.getKeyCode()==10){
            int codigo;
            try{
                codigo = Integer.parseInt(txtCodigo.getText());
            }catch(Exception e){
                return;
            }
            this.carregarDadosCampos(codigo);
        }
    }//GEN-LAST:event_txtCodigoKeyPressed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed

        txtCodigo.setEnabled(false);
        txtNome.setEnabled(true);
        txtCpf.setEnabled(true);
        txtDepartamento.setEnabled(true);

        txtNome.setText("");
        txtCpf.setText("");
        txtDepartamento.setText("");

        btnNovo.setEnabled(false);
        btnAlterar.setEnabled(false);
        btnRemover.setEnabled(false);
        btnSalvar.setEnabled(true);
        btnCancelar.setEnabled(true);

        txtNome.grabFocus();
        this.setModificacao("N");
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if (txtCodigo.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe um professor para ser alterado.");
        }else{
            txtCodigo.setEnabled(false);
            txtNome.setEnabled(true);
            txtCpf.setEnabled(true);
            txtDepartamento.setEnabled(true);

            btnNovo.setEnabled(false);
            btnAlterar.setEnabled(false);
            btnRemover.setEnabled(false);
            btnSalvar.setEnabled(true);
            btnCancelar.setEnabled(true);

            txtNome.grabFocus();
            this.setModificacao("A");
        }
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoverActionPerformed
        if (txtCodigo.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe um professor para ser removido.");
        }else{
            if (JOptionPane.showConfirmDialog(null, "Deseja remover o professor?")==0){
                Professor professor = new Professor(Integer.parseInt(txtCodigo.getText()),txtNome.getText(),txtCpf.getText(),txtDepartamento.getText());
                ProfessorDao professorDao = new ProfessorDaoImplJPA();
                
                TurmaDao turmaDao = new TurmaDaoImplJPA();
                List<Turma> listaTurmasDisciplina = turmaDao.pesquisarPorProfessor(professor);
                if (listaTurmasDisciplina.size()>0){
                    JOptionPane.showMessageDialog(null, "Impossível remover este professor, pois ele tem turmas cadastradas.");
                    return;
                }
                
                if (professorDao.remover(professor)==true){

                    txtCodigo.setEnabled(true);
                    txtNome.setEnabled(false);
                    txtCpf.setEnabled(false);
                    txtDepartamento.setEnabled(false);

                    txtCodigo.setText("");
                    txtNome.setText("");
                    txtCpf.setText("");
                    txtDepartamento.setText("");

                    btnNovo.setEnabled(true);
                    btnAlterar.setEnabled(false);
                    btnRemover.setEnabled(false);
                    btnSalvar.setEnabled(false);
                    btnCancelar.setEnabled(false);

                    JOptionPane.showMessageDialog(null, "Professor removido com sucesso.");
                }else{
                    JOptionPane.showMessageDialog(null, "Erro ao remover o professor.");
                }
            }
        }
    }//GEN-LAST:event_btnRemoverActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

        ProfessorDao alunoDao = new ProfessorDaoImplJPA();
        Professor professor;
        boolean salvo = false;

        if (txtNome.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe o nome do professor.");
            return;
        }

        if (txtCpf.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe o cpf do professor.");
            return;
        }
        
        if (txtDepartamento.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe o departamento ao qual o professor pertence.");
            return;
        }

        if (this.getModificacao().equals("A")){
            professor = new Professor(Integer.parseInt(txtCodigo.getText()),txtNome.getText(),txtCpf.getText(),txtDepartamento.getText());
            if (alunoDao.alterar(professor)==true){
                JOptionPane.showMessageDialog(null, "Professor alterado com sucesso.");
                salvo = true;
            }else{
                JOptionPane.showMessageDialog(null, "Erro ao alterar o professor.");
                salvo = false;
            }
        }else if (this.getModificacao().equals("N")){
            professor = new Professor(txtNome.getText(),txtCpf.getText(),txtDepartamento.getText());

            if (alunoDao.inserir(professor)==true){
                JOptionPane.showMessageDialog(null, "Professor inserido com sucesso.");
                salvo = true;
            }else{
                JOptionPane.showMessageDialog(null, "Erro ao inserir o professor.");
                salvo = false;
            }
        }

        if (salvo == true){
            txtCodigo.setEnabled(true);
            txtNome.setEnabled(false);
            txtCpf.setEnabled(false);
            txtDepartamento.setEnabled(false);

            txtCodigo.setText("");
            txtNome.setText("");
            txtCpf.setText("");
            txtDepartamento.setText("");

            btnNovo.setEnabled(true);
            btnAlterar.setEnabled(false);
            btnRemover.setEnabled(false);
            btnSalvar.setEnabled(false);
            btnCancelar.setEnabled(false);

            this.setModificacao("");

            txtCodigo.grabFocus();
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        txtCodigo.setEnabled(true);
        txtNome.setEnabled(false);
        txtCpf.setEnabled(false);
        txtDepartamento.setEnabled(false);

        txtCodigo.setText("");
        txtNome.setText("");
        txtCpf.setText("");
        txtDepartamento.setText("");

        btnNovo.setEnabled(true);
        btnAlterar.setEnabled(false);
        btnRemover.setEnabled(false);
        btnSalvar.setEnabled(false);
        btnCancelar.setEnabled(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnRemover;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblCpf;
    private javax.swing.JLabel lblDepartamento;
    private javax.swing.JLabel lblNome;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtCpf;
    private javax.swing.JTextField txtDepartamento;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables
}
